import React, { Component } from 'react'
import PropTypes from 'prop-types'
import './index.less'
export default class Footer extends Component {
  handleAllChange = (e)=>{
    // console.log(e.target.checked);
    if(e.target.checked == true){
      this.props.handleAllDone(true)
    }else{
      this.props.handleAllDone(false)
    }
  }
  handleAllMove = ()=>{
    let {todos,handleAllRemove} = this.props
    //将没有被完成的任务传给app组件
    handleAllRemove(todos.filter(item=>{return item.done == false}))
  }
  render() {
    const {todos} = this.props
    //处理选中状态为true的数据
    let doneCount = todos.reduce((pre,current)=>
       pre + (current.done ? 1:0)
    ,0)
    return (
      <div className='todo-footer'>
        <label>
            <input type="checkbox" onChange={this.handleAllChange} checked={doneCount==todos.length&&todos.length!==0?true:false}/>
        </label>
        <span>
            <span>已完成{doneCount}</span>/全部{todos.length}
        </span>
        <button className='btn btn-danger' onClick={this.handleAllMove}>清除已完成任务</button>
      </div>
    )
  }
  static propTypes = {
    todos:PropTypes.array.isRequired
  }
}
